package com.py.base.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.py.base.service.SysGeneratorService;
import com.py.base.utils.LayuiPageFactory;
import com.py.base.utils.LayuiPageInfo;
import com.py.base.utils.Page;
import com.py.base.utils.Query;
import com.py.base.utils.xss.XssHttpServletRequestWrapper;
import com.py.base.vo.TableVo;
 

/**
 * 代码生成器
 * 
 * @author xuxingrui/xucheng
 * @email xuqiao2016@126.com
 * @date 2017年9月19日 下午9:12:58
 */
@Controller
@RequestMapping("/generator")
public class SysGeneratorController {
	@Autowired
	private SysGeneratorService sysGeneratorService;
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/querytablelist")
	 
	public LayuiPageInfo list(TableVo tableVo){
		Page page = LayuiPageFactory.defaultPage();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("limit", page.getPage());
		params.put("page", page.getLimit());
		System.out.println("--tableVo--"+JSON.toJSONString(tableVo));
		if(tableVo!=null) {
			params.put("tableName", tableVo.getTableName());
		}
		//查询列表数据
		Query query = new Query(params);
		System.out.println("--params--"+JSON.toJSONString(params));
		System.out.println("--query--"+JSON.toJSONString(query));
		List<Map<String, Object>> list = sysGeneratorService.queryList(query);
		int total = sysGeneratorService.queryTotal(query);
		
		//PageUtils pageUtil = new PageUtils(list, total, page.getLimit(), page.getPage());
		return  LayuiPageFactory.createPageInfo(total,list);
		//return R.ok().put("page", pageUtil);
	}
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/querycolumnslist")
	 
	public LayuiPageInfo querycolumnslist(@RequestParam Map<String, Object> params){
	  
		Page page = LayuiPageFactory.defaultPage();
		 
		params.put("limit", page.getPage());
		params.put("page", page.getLimit());
		//查询列表数据
		Query query = new Query(params);
		 
		List<Map<String, Object>> list = sysGeneratorService.queryColumnsList(query);
		int total = sysGeneratorService.queryColumnsTotal(query);
		return  LayuiPageFactory.createPageInfo(total,list);
		
		//return R.ok().put("page", pageUtil);
	}
	/**
	 * 生成代码
	 */
	@RequestMapping("/code")
	public void code(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String[] tableNames = new String[]{};
		//获取表名，不进行xss过滤
		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		String tables = orgRequest.getParameter("tables");
		tableNames = JSON.parseArray(tables).toArray(tableNames);
		 
		byte[] data = sysGeneratorService.generatorCode(tableNames);
		String filename="";
		for(String tableName : tableNames){
			filename+=tableName;
		}
		response.reset();  
        response.setHeader("Content-Disposition", "attachment; filename=\""+filename+".zip\"");  
        response.addHeader("Content-Length", "" + data.length);  
        response.setContentType("application/octet-stream; charset=UTF-8");  
  
        IOUtils.write(data, response.getOutputStream());  
	}
	/**
	 * 生成代码
	 */
	@RequestMapping("/generatorTable")
	public void generatorTable(HttpServletRequest request, HttpServletResponse response) throws IOException{
	 
		String tableName = request.getParameter("tableName");
		byte[] data = sysGeneratorService.generatorCode(tableName);
		response.reset();  
        response.setHeader("Content-Disposition", "attachment; filename=\""+tableName+".zip\"");  
        response.addHeader("Content-Length", "" );  
        response.setContentType("application/octet-stream; charset=UTF-8");  
  
        IOUtils.write(data, response.getOutputStream());  
	}
}
